import 'package:flutter/material.dart';
import 'package:flutter_base/page/common/webview_widget.dart';
import 'package:flutter_html/flutter_html.dart';

///
/// @DIR_PATH：lib/page/home
/// @TIME：2024/5/3 8:52
/// @AUTHOR：starr
///
class WebViewPage extends StatefulWidget {
  const WebViewPage(
      {required this.webViewType,
      required this.loadResource,
      super.key,
      this.title,
      this.showTitle,
      this.jsChannelMap});

  //是否显示标题
  final bool? showTitle;

  //标题内容
  final String? title;

  //需要加载的内容类型
  final WebViewType webViewType;

  //需要加载的数据
  final String loadResource;

  //与js通信的channel集合
  final Map<String, JsChannelCallback>? jsChannelMap;
  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: (widget.showTitle ?? false)
              ? _buildAppBarTitle(widget.showTitle, widget.title)
              : null,
        ),
        body: SafeArea(
          child: WebViewWidget(
            webViewType: widget.webViewType,
            loadResource: widget.loadResource,
            jsChannelMap: widget.jsChannelMap,
          ),
        ));
  }

  Widget _buildAppBarTitle(bool? showTitle, String? title) {
    var show = showTitle ?? false;
    return show
        ? Html(
            data: title ?? "",
            style: {"html": Style(fontSize: FontSize(15))},
          )
        : const SizedBox.shrink();
  }

  String limitStr(String? content, {int limit = 15}) {
    if (content == null || content.isEmpty == true) {
      return "";
    }
    if (content.length > 15) {
      return content.substring(0, 15);
    } else {
      return content;
    }
  }
}
